package com.weidey.blog.controller;

import java.util.List;

import com.weidey.blog.domain.Label;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.weidey.common.annotation.Log;
import com.weidey.common.core.controller.BaseController;
import com.weidey.common.core.domain.AjaxResult;
import com.weidey.common.enums.BusinessType;
import com.weidey.blog.service.ILabelService;
import com.weidey.common.utils.poi.ExcelUtil;
import com.weidey.common.core.page.TableDataInfo;

/**
 * 文章标签Controller
 * 
 * @author weidey
 * @date 2024-09-09
 */
@RestController
@RequestMapping("/blog/label")
public class LabelController extends BaseController
{
    @Autowired
    private ILabelService blogLabelService;

    /**
     * 查询文章标签列表
     */
    @PreAuthorize("@ss.hasPermi('blog:label:list')")
    @GetMapping("/list")
    public TableDataInfo list(Label label)
    {
        startPage();
        List<Label> list = blogLabelService.selectBlogLabelList(label);
        return getDataTable(list);
    }

    /**
     * 导出文章标签列表
     */
    @PreAuthorize("@ss.hasPermi('blog:label:export')")
    @Log(title = "文章标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Label label)
    {
        List<Label> list = blogLabelService.selectBlogLabelList(label);
        ExcelUtil<Label> util = new ExcelUtil<Label>(Label.class);
        util.exportExcel(response, list, "文章标签数据");
    }

    /**
     * 获取文章标签详细信息
     */
    @PreAuthorize("@ss.hasPermi('blog:label:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(blogLabelService.selectBlogLabelById(id));
    }

    /**
     * 新增文章标签
     */
    @PreAuthorize("@ss.hasPermi('blog:label:add')")
    @Log(title = "文章标签", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Label label)
    {

        return toAjax(blogLabelService.insertBlogLabel(label));
    }

    /**
     * 修改文章标签
     */
    @PreAuthorize("@ss.hasPermi('blog:label:edit')")
    @Log(title = "文章标签", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Label label)
    {
        return toAjax(blogLabelService.updateBlogLabel(label));
    }

    /**
     * 删除文章标签
     */
    @PreAuthorize("@ss.hasPermi('blog:label:remove')")
    @Log(title = "文章标签", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable("ids") Long[] ids)
    {
        return toAjax(blogLabelService.deleteBlogLabelByIds(ids));
    }

    /**
     *  获取所有文章标签
     * @return
     */
    @PreAuthorize("@ss.hasPermi('blog:label:list')")
    @GetMapping("/list/all")
    public AjaxResult listAll(){
        List<Label> list = blogLabelService.selectAllBlogLabelList();
        return success(list);
    }
}
